package com.leon.androidcoursedesign.ui.fragments.noteFragment;

import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.leon.androidcoursedesign.R;
import com.leon.androidcoursedesign.adapters.NoteListAdapter;
import com.leon.androidcoursedesign.beans.Note;
import com.leon.androidcoursedesign.ui.dialogs.NoteDialog;
import com.leon.androidcoursedesign.ui.fragments.baseFragment.BaseFragment;
import com.leon.androidcoursedesign.ui.fragments.baseFragment.BindingUtils;
import com.leon.androidcoursedesign.utils.ValueUtils;
import com.leon.androidcoursedesign.utils.dataBaseUtils.NoteUtils;

import java.io.File;
import java.util.List;

public class NoteListFragment extends BaseFragment implements View.OnClickListener, NoteListAdapter.OnNoteItemListener {
    private RecyclerView note_list_fragment_recyclerView;
    private NoteUtils noteUtils;
    private List<Note> notes;

    private NoteListAdapter noteListAdapter;
    private NoteDialog noteDialog;
    private String operationId;

    @Override
    public int setLayout() {
        return R.layout.note_list_fragment;
    }

    @Override
    public void init() {
        operationId = ValueUtils.getLoginId(context);

        // 检查文件夹的存在状态
        checkFolder();
    }

    @Override
    public void initView(BindingUtils bindingUtils) {
        bindingUtils
                .setOnClickListener(R.id.note_list_fragment_imageView_back, this)
                .setOnClickListener(R.id.note_list_fragment_imageView_add_note, this);

        note_list_fragment_recyclerView = findView(R.id.note_list_fragment_recyclerView);
    }

    @Override
    public void initValues() {
        noteUtils = new NoteUtils(context);
        notes = noteUtils.queryAllNote(operationId);

        noteListAdapter = new NoteListAdapter(notes, context);
        noteListAdapter.setOnNoteItemListener(this);
        note_list_fragment_recyclerView.setAdapter(noteListAdapter);
        note_list_fragment_recyclerView.setLayoutManager(new LinearLayoutManager(context));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.note_list_fragment_imageView_add_note:
                Navigation.findNavController(view).navigate(R.id.action_noteListFragment_to_noteEditFragment);
                break;
            case R.id.note_list_fragment_imageView_back:
                getActivity().finish();
                break;
            default:
                break;
        }
    }

    private void checkFolder() {
        File filesDir = context.getFilesDir();

        File notes = new File(filesDir, "notes");
        Log.d("Leon", "checkFolder: " + notes.getPath());
        if (!notes.exists()) {
            notes.mkdirs();
        }
    }

    @Override
    public void renameNote(Note note) {
        noteDialog = new NoteDialog(context, "修改笔记标题", true);
        noteDialog.setOnCallback(new NoteDialog.OnCallback() {
            @Override
            public void callBackTitle(String noteTitle) {
                note.showName = noteTitle;
                noteListAdapter.replace(note);
                noteUtils.updateNote(note);
            }

            @Override
            public void saveNote() {

            }
        });
        noteDialog.show();
    }

    @Override
    public void deleteNote(String itemId, String noteId, int position) {
        // 从数据库中删除
        boolean deleteState = noteUtils.deleteNote(itemId, noteId, operationId);
        Toast.makeText(context, deleteState ? "删除成功" : "删除失败", Toast.LENGTH_SHORT).show();

        // 从RecyclerView中删除
        noteListAdapter.delete(position);

        // 删除本地note文本文件
        ValueUtils.deleteNote(context, noteId);
    }

    @Override
    public void onDestroy() {
        noteUtils.close();
        super.onDestroy();
    }
}